﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IBLL;
using Model;
using Comment;

namespace BLL
{
    public class BLLTBLogins : IBLLTBLogins
    {
        IDAL.IDALTBLogins dALTB = new DAL.DALTBLogins();
        /// <summary>
        ///注册
        /// </summary>
        /// <param name="logins"></param>
        /// <returns></returns>
        public OperResult CreateBLL(TBLogins logins)
        {
            if (string.IsNullOrWhiteSpace(logins.MobileNumber)) {
                return OperResult.Failed("手机号不能为空");
            }
            if (string.IsNullOrWhiteSpace(logins.Password)) {
                return OperResult.Failed("密码不能为空");
            }
            if (string.IsNullOrWhiteSpace(logins.UserName)) {
                return OperResult.Failed("用户名不能为空");
            }
            
            if (dALTB.SelectUserName(logins.UserName)) {
                return OperResult.Failed("用户名已存在");
            }
            if (dALTB.SelectMobileNumber(logins.MobileNumber)){
                return OperResult.Failed("手机号已存在");
            }
            SHAEncryption encryption = new SHAEncryption();
            logins.Password=encryption.SHA1Encrypt(logins.Password);
            try
            {
                dALTB.Create(logins);
            }
            catch (Exception ex)
            {

                return OperResult.Wrong(ex);
            }
            return OperResult.Succeed();
        }

        public OperResult<TBLogins> Select(string Password, string UserName)
        {
            SHAEncryption encryption = new SHAEncryption();
            var se=dALTB.SelectTiao(encryption.SHA1Encrypt(Password),UserName);
            return OperResult<TBLogins>.Succeed(se);

        }
    }
}
